package com.css.gistools.utils;

//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.geojson.geom.GeometryJSON;
import org.geotools.geometry.jts.Geometries;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.WKTReader;
import org.opengis.feature.Property;
import org.opengis.feature.simple.SimpleFeature;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.*;

import static org.locationtech.jts.io.WKTConstants.MULTIPOLYGON;

/**
 * 读取本地数据
 */
@Slf4j
public class ReadShp {
    private int featureSize=0;
    public static void main(String[] args) throws IOException {
//        readgeoShp("E:/zpdata/roadpolygon.shp");
      JSONObject allGeo=readAllShp("F:/20220410/xzqh/KMS.shp");

        System.out.println(allGeo.toJSONString());
//        IOUtils.writeJson(allGeo.toJSONString());
    }
    //构造geojson固定部分 传入featureJson 一个文件对应一个Geojson
    public static JSONObject buildGeoJson(JSONArray featuresJson){
        JSONObject Geojson=new JSONObject();
        Geojson.put("type","FeatureCollection");
        Geojson.put("features",featuresJson);
        return Geojson;
    }
    //构造Geojson的features部分 单个
    public static JSONObject buildFeature(Map geoObject,Map properties){
     JSONObject featureObject=new JSONObject();
      Map featureMap=new HashMap();
      featureMap.put("type","Feature");
      featureMap.putAll(geoObject);
      featureMap.put("properties",properties);
      featureObject.putAll(featureMap);
      return featureObject;
    }
    public static Map getGeoMap(String wkt) {
        Map<String,Object> geoMap=new HashMap<>();
        String json = null;
        try {
            WKTReader reader = new WKTReader();
            Geometry geometry = reader.read(wkt);
            StringWriter writer = new StringWriter();
            GeometryJSON g = new GeometryJSON();
            g.write(geometry, writer);
            geoMap.put("geometry", writer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return geoMap;
    }
   //    只读取geo信息 wkt
    public static List<String>readgeoShp(String shpPath){
        List<String>shpList=new ArrayList<>();
        SimpleFeatureCollection simpleFeatureCollection = null;
        try {
            //获取文件
            File file=new File(shpPath);
            // 读取到数据存储中
            FileDataStore dataStore = FileDataStoreFinder.getDataStore(file);
            // 获取特征资源
            SimpleFeatureSource simpleFeatureSource = dataStore.getFeatureSource();
            // 要素集合
            simpleFeatureCollection = simpleFeatureSource.getFeatures();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 获取要素迭代器
        SimpleFeatureIterator featureIterator = simpleFeatureCollection.features();
        while (featureIterator.hasNext()){
            // 要素对象
            SimpleFeature feature = featureIterator.next();
            Object  geometryText = feature.getDefaultGeometry();
            log.info( geometryText.toString());
            shpList.add(geometryText.toString());

        }
        featureIterator.close();
        return shpList;
    }
//读取全部shp
    public static JSONObject  readAllShp(String shpPath){
        JSONArray featureArray=new JSONArray();
//        List<String>shpList=new ArrayList<>();
        SimpleFeatureCollection simpleFeatureCollection = null;
        try {
//            要素合集
            //获取文件
            File file = new File(shpPath);
            // 读取到数据存储中
            ShapefileDataStore dataStore = (ShapefileDataStore) FileDataStoreFinder.getDataStore(file);
            dataStore.setCharset(Charset.forName("GBK"));
            // 获取特征资源
            SimpleFeatureSource simpleFeatureSource = dataStore.getFeatureSource();
            // 要素集合
            simpleFeatureCollection = simpleFeatureSource.getFeatures();
        }catch (IOException e) {
            e.printStackTrace();
        }
            SimpleFeatureIterator featureIterator = simpleFeatureCollection.features();
//            // 要素数量
//            int featureSize = simpleFeatureCollection.size();
//            log.info("要素数量"+featureSize);
            //创建properties  Map
            while (featureIterator.hasNext()){
                        // 要素对象
                SimpleFeature feature = featureIterator.next();
                Collection<Property>propertyCollection= (Collection<Property>) feature.getValue();
                //填充属性map
                Map<String,Object>properMap=new HashMap<>();
                for(Property property:propertyCollection){
                    if(property.getName().toString().equals("the_geom")){
                        continue;
                    }
                    properMap.put(property.getName().toString(),property.getValue());
                }
                //获取geo信息
                Object geometryText = feature.getDefaultGeometry();
                Map geoMap=getGeoMap(geometryText.toString());
                JSONObject featureObject=buildFeature(geoMap,properMap);
                featureArray.add(featureObject);
            }
            featureIterator.close();
          JSONObject GeoJson=buildGeoJson(featureArray);

       return GeoJson;
    }



}
